#!/bin/bash
# unraid-api-handler

unraid_binary_path="/usr/local/bin/unraid-api"
api_base_dir="/usr/local/unraid-api"
scripts_dir="/usr/local/share/dynamix.unraid.net/scripts"

# Ensure script permissions
if [ -d "$scripts_dir" ]; then
  chmod +x "$scripts_dir"/*.sh 2>/dev/null
fi

# Placeholder functions for plugin installation/uninstallation
install() {
  true
}

uninstall() {
  true
}

# Service control functions
start() {
  echo "Starting Unraid API service..."

  # Restore vendored API plugins if they were installed
  if [ -x "$scripts_dir/dependencies.sh" ]; then
    "$scripts_dir/dependencies.sh" restore || {
      echo "Failed to restore API plugin dependencies! Continuing with start, but API plugins may be unavailable."
    }
  else
    echo "Warning: dependencies.sh script not found or not executable"
  fi

  # Create log directory if it doesn't exist
  mkdir -p /var/log/unraid-api

  # Copy env file if needed
  if [ -f "${api_base_dir}/.env.production" ] && [ ! -f "${api_base_dir}/.env" ]; then
    cp "${api_base_dir}/.env.production" "${api_base_dir}/.env"
  fi

  # Start the flash backup service if available and connect plugin is enabled
  if [ -x "/etc/rc.d/rc.flash_backup" ]; then
    # Check if connect plugin is enabled before starting flash backup
    if [ -x "$scripts_dir/api_utils.sh" ] && "$scripts_dir/api_utils.sh" is_api_plugin_enabled "unraid-api-plugin-connect"; then
      echo "Starting flash backup service..."
      /etc/rc.d/rc.flash_backup start
    fi
  fi

  # Start the API service
  if [ -x "${unraid_binary_path}" ]; then
    "${unraid_binary_path}" start
    return $?
  else
    echo "Error: Unraid API binary not found or not executable at ${unraid_binary_path}"
    return 1
  fi
}

stop() {
  echo "Stopping Unraid API service..."
  if [ -x "${unraid_binary_path}" ]; then
    "${unraid_binary_path}" stop
    return $?
  else
    echo "Error: Unraid API binary not found or not executable at ${unraid_binary_path}"
    return 1
  fi
}

restart() {
  stop
  sleep 2
  start
}

status() {
  if [ -x "${unraid_binary_path}" ]; then
    "${unraid_binary_path}" status
    return $?
  else
    echo "Error: Unraid API binary not found or not executable at ${unraid_binary_path}"
    return 1
  fi
}

case "$1" in
'start')
  start
  ;;
'stop')
  stop
  ;;
'restart' | 'reload')
  restart
  ;;
'status')
  status
  ;;
'install')
  install "$2"
  ;;
'uninstall')
  uninstall
  ;;
'ensure')
  if [ -x "$scripts_dir/dependencies.sh" ]; then
    # First clean up old dependencies
    "$scripts_dir/dependencies.sh" cleanup
    # Then ensure new dependencies are installed
    "$scripts_dir/dependencies.sh" ensure "$2"
  else
    echo "Error: dependencies.sh script not found or not executable"
    exit 1
  fi
  ;;
'cleanup-dependencies')
  if [ -x "$scripts_dir/dependencies.sh" ]; then
    "$scripts_dir/dependencies.sh" cleanup
  else
    echo "Error: dependencies.sh script not found or not executable"
    exit 1
  fi
  ;;
'archive-dependencies')
  if [ -x "$scripts_dir/dependencies.sh" ]; then
    "$scripts_dir/dependencies.sh" archive
  else
    echo "Error: dependencies.sh script not found or not executable"
    exit 1
  fi
  ;;
'redownload-vendor-archive')
  if [ -x "$scripts_dir/dependencies.sh" ]; then
    if [ -n "$2" ] && [ -n "$3" ]; then
      "$scripts_dir/dependencies.sh" redownload "$2" "$3"
    else
      echo "Usage: $0 redownload-vendor-archive <archive_path> <version>"
      exit 1
    fi
  else
    echo "Error: dependencies.sh script not found or not executable"
    exit 1
  fi
  ;;
*)
  # Pass all other commands to unraid-api
  "${unraid_binary_path}" "$@"
  ;;
esac
